﻿using AutoMapper;
using Common.Domain;
using Common.Infrastructure.Repository;
using Common.Services;
using MediatR;

namespace Business.ReadAPI.Application.Products
{
    public class ProjectReverseRequestHandler : IRequestHandler<ProjectReverseRequestModel, APIResult>
    {
        private IBaseRepository<Product> productRepository;
        private IBaseRepository<Food> foodRepository;
        private readonly IMapper mapper;
        public ProjectReverseRequestHandler(IBaseRepository<Product> productRepository, IMapper mapper, IBaseRepository<Food> foodRepository)
        {
            this.productRepository = productRepository;
            this.mapper = mapper;
            this.foodRepository = foodRepository;
        }
        public Task<APIResult> Handle(ProjectReverseRequestModel request, CancellationToken cancellationToken)
        {
            APIResult result = new APIResult();
            result.Code = EnumCode.SearchSuccess;
            result.Msg = "查询成功";

            var list = from a in productRepository.GetAll()
                       join b in foodRepository.GetAll() on a.FoodId equals b.FoodId
                       where a.IsDel == false
                       select new ProjectReverseResponseModel
                       {
                           ProductId = a.ProductId,
                           ProductCode = a.ProductCode,
                           FoodId = a.FoodId,
                           FoodName = b.FoodName,
                           ProductionBatch = a.ProductionBatch,
                           ShelfLife = a.ShelfLife,
                           LicenseNumber = a.LicenseNumber,
                           Enterprise = a.Enterprise,
                           Creator = a.Creator,
                           CreateTime = a.CreateTime,
                           Updator = a.Updator,
                           UpdateTime = a.UpdateTime,
                           Deletor = a.Deletor,
                           DeleteTime = a.DeleteTime,
                           PhotoUrl = a.PhotoUrl
                       };
            if (request.ProductId != 0)
            {
                list = list.Where(x => x.ProductId == request.ProductId);
            }
            result.Data = list;

            return Task.FromResult(result);
        }
    }
    public class ProjectReverseRequestModel:IRequest<APIResult>
    {
        public int ProductId { get; set; }
    }
    public class ProjectReverseResponseModel
    {
        public int ProductId { get; set; }
        public string ProductCode { get; set; }
        public int FoodId { get; set; }
        public string FoodName { get; set; }
        public string ProductionBatch { get; set; }
        public int ShelfLife { get; set; }
        public string LicenseNumber { get; set; }
        public string Enterprise { get; set; }
        public string Creator { get; set; }
        public DateTime CreateTime { get; set; } = DateTime.Now;
        public string Updator { get; set; }
        public DateTime UpdateTime { get; set; } = DateTime.Now;
        public string Deletor { get; set; }
        public DateTime DeleteTime { get; set; } = DateTime.Now;
        public string PhotoUrl { get; set; }
        public bool IsDel { get; set; }
    }
}
